import Vue from 'vue'
import VueRouter from 'vue-router'
const company = () => import('../views/company') // 公司组件
const question = () => import('../views/question') // 面试宝典页面组件
const find = () => import('../views/find') // 发现页面组件
const my = () => import('../views/my') // 我的页面组件
const search = () => import('../views/search') // 搜索组件
const experienceAdd = () => import('../views/find/experienceAdd.vue') // 面试分享页面组件
const chart = () => import('../views/chart') // 图表组件
const info = () => import('../views/userInfo') // 用户信息
const message = () => import('../views/my/message.vue')
const companyDetail = () => import('../views/companyDetail') // 公司详情组件
const companyPosition = () => import('../views/companyPosition') // 公司地址组件
const positionDetail = () => import('../views/positionDetail') // 详细地址组件
const interview = () => import('../views/interview/') // 面试组件
// const answerSheet = () => import('../views/answerSheet')
const login = () => import('../views/login') // 登录组件
const editInfo = () => import('../views/editInfo') // 修改组件
const technicDetail = () => import('../views/technicDetail')
const technicList = () => import('../views/technicList') // 面试技巧列表页面组件
const shareDetail = () => import('../views/shareDetail')
const shareList = () => import('../views/shareList')
const nic = () => import('../views/my/comm/nic.vue')
const jianj = () => import('../views/my/comm/jianj.vue')
const upload = () => import('../views/my/comm/upload.vue')
const gangw = () => import('../views/my/comm/gangw.vue')
const xiaoxi = () => import('../views/my/comm/xiaoxi.vue')
const duix = () => import('../views/my/comm/duix.vue')
const subject = () => import('../views/question/subject/subject.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/company',
    component: company,
    meta: {
      showTabbar: true
    }
  },
  {
    path: '/',
    redirect: '/find'
  },
  {
    path: '/companyDetail/:id',
    component: companyDetail
  },
  {
    path: '/companyPosition/:id',
    component: companyPosition
  },
  {
    path: '/positionDetail/:id',
    component: positionDetail
  },
  { path: '/', redirect: 'question' }, // 重定向 写完后要删掉
  {
    path: '/question',
    component: question,
    meta: {
      showTabbar: true
    }
  },
  {
    path: '/interview/:id',
    component: interview
  },
  // {
  //   path: '/answerSheet',
  //   component: answerSheet
  // },
  {
    path: '/find',
    component: find,
    meta: {
      showTabbar: true
    }
  },
  {
    path: '/search/:tag',
    component: search
  },
  {
    path: '/experienceAdd',
    component: experienceAdd
  },
  {
    path: '/chart',
    component: chart
  },
  {
    path: '/my',
    component: my,
    meta: {
      showTabbar: true
    }
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/info',
    component: info
  },
  {
    path: '/message',
    component: message
  },
  {
    path: '/editInfo',
    component: editInfo
  },
  {
    // 文章详情
    path: '/technicDetail/:id',
    component: technicDetail
  },
  {
    // 文章列表
    path: '/technicList',
    component: technicList
  },
  {
    // 文章列表
    path: '/shareList',
    component: shareList
  },
  {
    // 面经分享详情
    path: '/shareDetail/:id',
    component: shareDetail
  },
  {
    // 修改昵称
    path: '/nic',
    component: nic
  },
  {
    // 修改个人简介
    path: '/jianj',
    component: jianj
  },
  {
    // 修改图片
    path: '/upload',
    component: upload
  },
  {
    // 我的岗位
    path: '/gangw',
    component: gangw
  },
  {
    // 我的消息
    path: '/xiaoxi',
    component: xiaoxi
  },
  {
    // 对象
    path: '/duix',
    component: duix
  },
  {
    path: '/subject/:city/:type',
    component: subject
  }
]

// 处理路由异常
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

// 跳转到登录页逻辑
VueRouter.prototype.toLogin = function (fromPath) {
  // 逻辑判断 写完后解开
  // if (fromPath) {
  //   return this.push(`/login?_redirect=${fromPath}`)
  // }
  // this.push(`/login?_redirect=${this.currentRoute.fullPath}`)
}
const router = new VueRouter({
  routes
})

router.beforeEach(async (to, from, next) => {
  // 如果token存在 尝试获取一下用户信息
  // 添加window的滚动获取
  from.meta.scrollTop =
    document.body.scrollTop || document.documentElement.scrollTop || 0
  next()
})
// 路由后置守卫
router.afterEach((to, from) => {
  // 如果发现路由元中有该路由的信息就跳到相应的位置
  if (to.query.isScroll) {
    setTimeout(() => {
      window.scrollTo(0, to.meta.scrollTop || 0)
    }, 0)
  } else {
    setTimeout(() => {
      window.scrollTo(0, 0)
    }, 0)
  }
  // } else {
  // if (to.meta.scrollTop) {
  //   console.log('to.meta.scrollTop:', to.meta.scrollTop)
  //   setTimeout(() => {
  //     window.scrollTo(0, to.meta.scrollTop)
  //   }, 300)
  // } else {
  // setTimeout(() => {
  //   window.scrollTo(0, 0)
  // }, 300)
  // }
  // }
})

export default router
